package com.cn.dai.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.MapUtils;
import com.cn.dai.common.JwtToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.ibatis.util.MapUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class JwtUtil {
    @Value("${JWT.SK}")
    private String JWT_Sk;
    @Value("{JWT.EXPIRATION}")
    private String EXPIRATION;

    public JwtToken parseJwt(String token){
        Claims body = Jwts.parser().setSigningKey(JWT_Sk).parseClaimsJws(token).getBody();
        JwtToken jwtToken = new JwtToken();
        jwtToken.setPhone(body.get("",String.class));
        jwtToken.setId(body.get("",String.class));
        jwtToken.setName(body.get("",String.class));
        jwtToken.setAuth(body.get("",String.class));
        return jwtToken;
    }
    public String buildJwt(JwtToken jwtToken){
        /*return Jwts.builder()
                .setHeaderParam("typ","jwt")
                .setHeaderParam("alg","HS256")
                .setClaims(BeanMap.create(jwtToken))
                .setExpiration(new Date(System.currentTimeMillis()+minute*60*1000))
                .signWith(SignatureAlgorithm.HS256,SECRET_KEY)
                .compact();*/
        return Jwts.builder()
                .setSubject(jwtToken.getName()) // 设置主题
                .setIssuedAt(new Date()) // 设置签发时间
                .setClaims(BeanMap.create(jwtToken))//组成参数
                .setExpiration(new Date(System.currentTimeMillis() + Long.parseLong(EXPIRATION))) // 设置过期时间
                .signWith(SignatureAlgorithm.HS256, JWT_Sk) // 设置签名算法和密钥
                .compact(); // 生成 JWT
    }
}
